﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ConvertorRaw2Vector
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            /*Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            */

            CFlightDB FlightDB = new CFlightDB();
            List<int> FlightID = new List<int>();
            System.IO.StreamWriter Log = new System.IO.StreamWriter("ConvertorRaw2Vector Log.txt", true);
            System.IO.StreamWriter ErrorLog;
            bool Fail = false;

            if (args.Length != 1)
            {
                Console.WriteLine("One command line argument is expected");
                return;
            }
            else
            {
                FlightID.Add(Convert.ToInt32(args[0])); // лист с id полетов сырых
                DateTime StartTime = DateTime.Now;

                Log.Write("Converting raw values of the flight #{0}. Started at {1}... ", FlightID[0], StartTime);
                Log.Flush();
                try
                {
                    FlightDB.ConvertRawFlight(FlightID);
                }
                catch (Exception e)
                {
                    if (e.Message != "Fail")
                    {
                        ErrorLog = new System.IO.StreamWriter("ConvertorRaw2Vector ErrorLog.txt", true);
                        ErrorLog.WriteLine("***********");
                        ErrorLog.WriteLine("Something uexpected has happend converting flight {2}...\n\t{0}\n\t{1}", e.Message, e.StackTrace, FlightID[0]);
                        ErrorLog.Close();
                    }
                    Fail = true;
                }

                if (!Fail)
                {
                    Console.WriteLine("Converting raw values of the flight #{0}. Started at {1}, finished at {2}", FlightID[0], StartTime, DateTime.Now);
                    Log.WriteLine("Finished at {0}", DateTime.Now);
                }
                else
                {
                    Console.WriteLine("Converting raw values of the flight #{0}. Started at {1}, failed at {2}. See ErrorLog", FlightID[0], StartTime, DateTime.Now);
                    Log.WriteLine("Failed at {0}. See ErrorLog", DateTime.Now);

                    SqlConnection m_SQLConnection = new SqlConnection("Data Source = 10.5.5.234; Initial Catalog = FlightDB; USER ID = admin; PASSWORD = 123; Connection Timeout = 2147483647");
                    SqlCommand m_SQLCommand = new SqlCommand("", m_SQLConnection);
                    m_SQLCommand.CommandTimeout = 600;

                    // Delete all values (if they are in the DB) in case of a fail
                    m_SQLConnection.Open();
                    m_SQLCommand.CommandText = String.Format("EXECUTE DeleteFromValue {0}", FlightID[0]);

                    try
                    {
                        m_SQLCommand.ExecuteNonQuery();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Exception of {0} type was thrown during executing query", e.GetType());
                        Log.WriteLine("\tApparantly some registration vectors of the flight #{0} were not deleted from data base", FlightID[0]);
                    }


                    Console.WriteLine("All registration vectors of the flight #{0} were deleted from data base", FlightID[0]);

                    m_SQLConnection.Close();
                }

                Log.Close();
            }
        }
    }
}
